﻿#region License

// // -------------------------------------------------------------------------------------
// // This information refers to RabbitMQHandler.cs
// // 
// // Copyright 2012 ELEN7046 Group 2
// // 
// // Last Modified on: 2012-06-10
// // 
// // Contributors:
// // 
// // Amith Sewnarain
// // Elmar Vosloo
// // Jason Thorne
// // Satish Jewan
// // 
// // Wits University 
// // School of Electrical & Information Engineering
// // Software Engineering Tools and Techniques
// // 
// // -------------------------------------------------------------------------------------
// // 
// // This file (RabbitMQHandler.cs) is part of the DishDash Event Visualization System.
// // 
// // DishDash is free software: you can redistribute it and/or modify
// // it under the terms of the GNU General Public License as published by
// // the Free Software Foundation, either version 3 of the License, or
// // (at your option) any later version.
// // 
// // DishDash is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// // GNU General Public License for more details.
// // 
// // You should have received a copy of the GNU General Public License
// // along with DishDash.  
// // If not, see <http://www.gnu.org/licenses/>.
// // -------------------------------------------------------------------------------------

#endregion

#region Using Directives

using System;
using System.Text;
using MessageAPI.Core;
using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;

#endregion

namespace RabbitMQIntegration
{
    public class RabbitMQHandler : RabbitMQBase, IDistributedCommunicationHandler
    {
        public RabbitMQHandler() : base()
        {
        }

        #region IDistributedCommunicationHandler Members

        /// <summary>
        ///   Sends the message.
        /// </summary>
        /// <param name="category"> The event. </param>
        /// <param name="textMessage"> The text message. </param>
        public void SendMessage(string category, string textMessage)
        {
            byte[] message = Encoding.UTF8.GetBytes(textMessage);
            RoutingKey = category;

            try
            {
                IBasicProperties basicProperties = Model.CreateBasicProperties();

                basicProperties.SetPersistent(true);
                Model.BasicPublish(ExchangeName, category, basicProperties, message);
                //Model.ConfirmSelect();
                Model.WaitForConfirmsOrDie();
                Model.Close();
                Connection.Close();
            }
            catch (OperationInterruptedException)
            {
                Console.WriteLine("Error: Connection to broker service interrupted");
                throw;
            }
        }

        #endregion
    }
}